﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Manager_Sanpham1_Controls_UC_Sualoaisanpham : System.Web.UI.UserControl
{
    BUS_Manufacturer data = new BUS_Manufacturer();
    DTO_Manufacturer obj = new DTO_Manufacturer();
    protected void Page_Load(object sender, EventArgs e)
    {   
        if (!IsPostBack)
        {
            hplXemloaiSP.NavigateUrl = "../Loaisanpham.aspx?id_nsx=" + Request.QueryString["id_nsx"];
            hplThemsanpham.NavigateUrl = "../Themsanpham.aspx?id_nsx=" + Request.QueryString["id_nsx"];
            hplSualoiSP.NavigateUrl = "../Sualoaisanpham.aspx?id_nsx=" + Request.QueryString["id_nsx"];
            string id_nsx = Request.QueryString["id_nsx"].ToString();
            RP_Sualoaisanpham.DataSource = data.Manufacturer_GetByTop(0, "Id=" + id_nsx, "[Order]");
            RP_Sualoaisanpham.DataBind();
            
           
        }
    }


    public string Thongbao(string text)
    {
        return "<script>alert('" + text + "')</script>";
    }
    public string Confirm(string text)
    {
        return "<script>confirm('"+ text +"')</script>";
    }
    public bool Mo(bool Status)
    {
        if (Status)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public bool Dong(bool Status)
    {
        if (Status)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    public void UploadAnh()
    {
        foreach (RepeaterItem item in RP_Sualoaisanpham.Items)
        {
            FileUpload FU = (FileUpload)item.FindControl("FileUpload1");
            if (FU.HasFile)
            {
                FU.SaveAs(Server.MapPath("~/Images/product/" + FU.FileName));
                obj.Image = "Images/product/" + FU.FileName;
            }
        }
        
    }
    protected void btnSualoaisanpham_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in RP_Sualoaisanpham.Items)
        {
            string id_nsx = Request.QueryString["id_nsx"].ToString();
            TextBox tbTennhasanxuat = (TextBox)item.FindControl("txtTennhasanxuat");
            //TextBox tbHinhanh = (TextBox)item.FindControl("txtHinhanhloaisanpham");
            Label lUrlImage = (Label)item.FindControl("LblHinhanh");
            Image IHinhanh = (Image)item.FindControl("Hinhanh");
            //string a = IHinhanh.ImageUrl.ToString();
            //Response.Write(lUrlImage.Text);
            string tTennsx = tbTennhasanxuat.Text;
            //string tHinhanh = tbHinhanh.Text;
            bool status = true;
            RadioButton RD1 = (RadioButton)item.FindControl("RdMo");
            RadioButton RD2 = (RadioButton)item.FindControl("RdDong");
            FileUpload FU = (FileUpload)item.FindControl("FileUpload1");
            if (RD1.Checked == true)
            {
                status = true;
            }
            if (RD2.Checked == true)
            {
                status = false;
            }
            if (tTennsx.Length == 0)
            {
                Alert.Text = Thongbao("Bạn chưa nhập tên nhà sản xuất!!!");
                //obj.Name = tTennsx;
                //Response.Redirect("../Sanpham/Sualoaisanpham.aspx?id_nsx=" + Request.QueryString["id_nsx"]);
                //Page.Response.Redirect(Page.Request.Url.ToString(), true);
                //tTennsx = " ";
            }
            if (FU.FileName.Length == 0)
            {
                obj.Image = lUrlImage.Text;

            }
            else if (FU.FileName.Length > 0)
            {
                UploadAnh();
                //obj.Name = tTennsx;
                //obj.Status = status;
                //data.Manufacturer_UpdateByAll(id_nsx, obj);
                //Alert.Text = Thongbao("Sửa thành công");
                //Response.Redirect("../Sanpham/Loaisanpham.aspx?id_nsx=" + Request.QueryString["id_nsx"]);

            }
            obj.Name = tTennsx;
            obj.Status = status;
            data.Manufacturer_UpdateByAll(id_nsx, obj);
            Response.Redirect("../Sanpham/Loaisanpham.aspx?id_nsx=" + Request.QueryString["id_nsx"]);
        }
    }
}